//
//  NSObject+ZBTopViewController.m
//  PaoPao
//
//  Created by xzb on 2018/5/12.
//  Copyright © 2018年 paopao. All rights reserved.
//

#import "NSObject+ZBTopViewController.h"

@implementation NSObject (ZBTopViewController)

- (UIViewController *)ZB_topViewController
{
    UIViewController *resultVC;
    resultVC = [self ZB_topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self ZB_topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)ZB_topViewController:(UIViewController *)vc
{
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self ZB_topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self ZB_topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

- (UINavigationController *)ZB_topNavigationController
{
    return [self ZB_topViewController].navigationController;
}

@end
